package com.zzyl.controller.security;

import com.zzyl.base.ResponseResult;
import com.zzyl.dto.ResourceDto;
import com.zzyl.service.ResourceService;
import com.zzyl.utils.MgtThreadLocal;
import com.zzyl.vo.MenuVo;
import com.zzyl.vo.ResourceVo;
import com.zzyl.vo.TreeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/resource")
@Api(tags = "资源管理")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    @GetMapping("/menus")
    @ApiOperation("我的菜单")
    public ResponseResult<List<MenuVo>> menus() {
   //     Long userId = 1671403256519078138L;
        // 动态获取当前登陆用户id
        Long userId = MgtThreadLocal.getUserId();
        List<MenuVo> menus = resourceService.menus(userId);
        return ResponseResult.success(menus);
    }

    /**
     * 资源列表
     */
    @PostMapping(path = "/list")
    @ApiOperation("查询资源列表")
    public ResponseResult<List<ResourceVo>> findList(@RequestBody ResourceDto resourceDto) {
        List<ResourceVo> list=resourceService.findList(resourceDto);
        return ResponseResult.success(list);
    }

    /**
     * 资源树形
     */
    @PostMapping(path = "/tree")
    @ApiOperation("资源树形")
    public ResponseResult<TreeVo> findTree(@RequestBody ResourceDto resourceDto) {
       TreeVo treeVo = resourceService.findTree(resourceDto);
       return ResponseResult.success(treeVo);
    }

    /**
     * 添加资源
     */
    @PutMapping
    @ApiOperation("新增资源")
    public ResponseResult<Void> addResource(@RequestBody ResourceDto resourceDto){
        resourceService.addResource(resourceDto);
        return ResponseResult.success();
    }

    /**
     * 启用禁用
     */
    @PostMapping(path = "/enable")
    @ApiOperation("启用/禁用")
    public ResponseResult<Void> modifyResourceStatus(@RequestBody ResourceDto resourceDto){
        resourceService.modifyResourceStatus(resourceDto);
        return ResponseResult.success();
    }

    /**
     * 删除菜单
     */
    @DeleteMapping(path = "/{menuId}")
    @ApiOperation("删除菜单")
    public ResponseResult<Void> deleteResource(@PathVariable String menuId){
        resourceService.deleteResource(menuId);
        return ResponseResult.success();
    }

    /**
     * 编辑菜单
     */
    @PatchMapping
    @ApiOperation("编辑菜单")
    public ResponseResult<Void> updateResource(@RequestBody ResourceDto resourceDto){
        resourceService.updateResource(resourceDto);
        return ResponseResult.success();
    }
}
